package afinadroid.salesforce.test401;


import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

import afinadroid.magalindo8.salesforce401test.R;
import afinadroid.salesforce.test401.utils.TestApplication;
import afinadroid.salesforce.test401.utils.Utils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FinalActivity extends Activity {

	// Variables publicidad
	private AdView adView;
	private static String UNIT_ID;
	private InterstitialAd interstitial;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_final);
		
		iniciarInterstitial();

		TextView questionSelectedText = (TextView) findViewById(R.id.question_selected_text);
		TextView correctText = (TextView) findViewById(R.id.correct_answers_text);
		TextView wrongText = (TextView) findViewById(R.id.wrong_answers_text);
		TextView passText = (TextView) findViewById(R.id.pass_text);

		questionSelectedText.setText(questionSelectedText.getText() + " "
				+ TestApplication.preguntas_seleccionadas);
		correctText.setText(correctText.getText() + " "
				+ TestApplication.acertadas);
		wrongText.setText(wrongText.getText() + " " + TestApplication.falladas);

		int porcentaje = (TestApplication.acertadas * 100)
				/ TestApplication.preguntas_seleccionadas;
		if (porcentaje < 68) {
			passText.setTextColor(getResources().getColor(R.color.red_wrong));
			passText.setText(this.getString(R.string.exam_failed_final) + " "
					+ porcentaje + "%");
		} else {
			passText.setTextColor(getResources()
					.getColor(R.color.green_correct));
			passText.setText(this.getString(R.string.exam_passed_final) + " "
					+ porcentaje + "%");
		}

	}

	public void startAgain(View view) {
		Utils.resetTestApplication();
		finish();
	}

	// -----------------------------------------------------------------
	// METODOS PUBLICIDAD
	// -----------------------------------------------------------------

	public void iniciarAnuncio() {
		// Obtener el id de los anuncios
		UNIT_ID = getString(R.string.ad_banner_test);

		// Create the adView.
		// adView = new AdView(QuestionActivity.this, AdSize.BANNER, UNIT_ID);
		adView = new AdView(FinalActivity.this);
		adView.setAdUnitId(UNIT_ID);
		adView.setAdSize(AdSize.BANNER);

		// Add the AdView to the view hierarchy. The view will have no size
		// until the ad is loaded.
		LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutAnuncio);
		layout.addView(adView);

		// Create an ad request. Check logcat output for the hashed device ID to
		// get test ads on a physical device.
		AdRequest adRequest = new AdRequest.Builder().addTestDevice(
				AdRequest.DEVICE_ID_EMULATOR)
		// .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
				.build();

		// Start loading the ad in the background.
		adView.loadAd(adRequest);
		// adView.loadAd(new AdRequest());
	}

	public void iniciarInterstitial() {
		// Obtener el id de los anuncios
		UNIT_ID = getString(R.string.ad_final_interstitial);
		
		// Create the interstitial.
		interstitial = new InterstitialAd(this);
		interstitial.setAdUnitId(UNIT_ID);

		// request test interstitial ads
        AdRequest adRequestInterstitial = new AdRequest.Builder()
//            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//            .addTestDevice(testDeviceId)
            .build();


		// Begin loading your interstitial.
		interstitial.loadAd(adRequestInterstitial);
		
		interstitial.setAdListener(new AdListener() {
	          public void onAdLoaded(){
	               displayInterstitial();
	          }
		});

	}

	// Invoke displayInterstitial() when you are ready to display an
	// interstitial.
	public void displayInterstitial() {
		if (interstitial.isLoaded()) {
			interstitial.show();
		}
	}

}
